package com.example.rxbus;

import rx.Observable;
import rx.Subscription;
import rx.functions.Action1;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
import rx.subjects.Subject;


public class RxBus {
    private static volatile RxBus rxBus;
    private final Subject<Object, Object> subject = new SerializedSubject<>(PublishSubject.create());

    private RxBus() {
    }

    public static RxBus getInstance() {
        if (rxBus == null) {
            synchronized (RxBus.class) {
                if (rxBus == null) {
                    rxBus = new RxBus();
                }
            }
        }
        return rxBus;
    }

    public void post(Object ob) {
        subject.onNext(ob);
    }

    public <T> Observable<T> toObservable(Class<T> eventType) {
        return subject.ofType(eventType);
    }

    public static void main(String[] args) {


        Subscription subscription = RxBus.getInstance().toObservable(MessageEvent.class).subscribe(new Action1<MessageEvent>() {
            @Override
            public void call(MessageEvent messageEvent) {
                if (messageEvent != null) {
                    System.out.println(messageEvent.getMessage());
                }
            }
        });

        RxBus.getInstance().post(new MessageEvent("asdasd"));
    }
}
